package cc.iotkit.plugins.soulcall.service;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;

import javax.crypto.SecretKey;


import cc.iotkit.plugins.soulcall.parse.LinxiContext;
import cc.iotkit.plugins.soulcall.parse.OtherJsonRequest;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.crypto.asymmetric.Sign;
import cn.hutool.crypto.asymmetric.SignAlgorithm;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class LinxiSignHelper {

    public static String ALARM_PATH = "/linxi/alarm.action";
    public static String EVENT_PATH = "/linxi/event.action";

    public static String ADD_DEVICE = "/other/addDevice";
    public static String DEL_DEVICE = "/other/delDevice";


    private static final String SIGN = "sign";
    //	private static final String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3knPd7x8nBRiIlO9qY149ZKfbW3kvnWtsYMAhAd/WDJn1/7wEgc4mfzJqv6hdwsu919y+dC7JKRkf4Rgfw/59HWdR0V/4QtYLcORZc581FxZp4L/ea5Hov1heYDWt1NwIWoyzZMEYOeIWinKSgnmQYhfLvgVOHngvtukD7VG+n2gY1Sy8w5uHbHzTMWcB6ebA7KsKV9eAevX6O/pxJG6TiXdPitpjn9eYPIwnECixMXRkzV3Tc477ulvV3lUgP6+oL6Fc1HRzmy9Nkek5C+1fDb9kd2/4ZfoNArZ7E15FPFZwbQmsIpxKzkGNUi2OgSJLzEMAxOMbzDZSxn7NcAVawIDAQAB";
//	private static final String privateKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCcHtWkttor93nIga8rVq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Yez2G72iIunyPfJ3Odzs3NSPVeUxpBjzdzzOGnMpwtsiSLX5PWNsUG6Ax977pubPG7xbp2P9Cy92SEFEb8Yr36poFOK+UgR42u59F85kTg2UZyF9KTUfbPoFJ3lUJCohJn/RnUeGWq2DNAoGAEkfLQcGvUT1yRdz7cubb7uAa08mYquNCx98VeL3xBozwIOJHs6ZEEjti75gKNS+SrQBrDQqHh7YDJgRnmthpuZbi02peEvO107pxtR3HAyG6+RvS9w8KHTfwl699ppnEkjkmQL7Q8Jwc0ngPrxqWsK4q8fG9ihVfTI/PnR5HpasCgYB9/ehwBktosTmclJ3Gjquywu1IfSImJeid8SA7e3D6ZISpNFS0dEY3GStvB7+V2Oci/RrsfJlZW0frVfR9AcZBdxnvfXbrc6MRL5e/jENePVPRSNfNb0uPa0Qb+iPCFbswSHVjaySu1U6b3hX2Acx75MXQu7GCCNLVqsTGtVio4w==";
//	private static final String appid = "vddaC17dq1hmc";
    private static SecretKey key = SecureUtil.generateKey("AES", 128);

    public static Map<Long, LinxiContext> contextMap = new ConcurrentHashMap<>();

    static {
        contextMap.put(202L, new LinxiContext("223", "117.761479","黄山三路邹平市综合为老服务中心测试","36.889968","MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3knPd7x8nBRiIlO9qY149ZKfbW3kvnWtsYMAhAd/WDJn1/7wEgc4mfzJqv6hdwsu919y+dC7JKRkf4Rgfw/59HWdR0V/4QtYLcORZc581FxZp4L/ea5Hov1heYDWt1NwIWoyzZMEYOeIWinKSgnmQYhfLvgVOHngvtukD7VG+n2gY1Sy8w5uHbHzTMWcB6ebA7KsKV9eAevX6O/pxJG6TiXdPitpjn9eYPIwnECixMXRkzV3Tc477ulvV3lUgP6+oL6Fc1HRzmy9Nkek5C+1fDb9kd2/4ZfoNArZ7E15FPFZwbQmsIpxKzkGNUi2OgSJLzEMAxOMbzDZSxn7NcAVawIDAQAB",
                "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiA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12L8vqNnI78zRTtJ0aZXAHKj8VYez2G72iIunyPfJ3Odzs3NSPVeUxpBjzdzzOGnMpwtsiSLX5PWNsUG6Ax977pubPG7xbp2P9Cy92SEFEb8Yr36poFOK+UgR42u59F85kTg2UZyF9KTUfbPoFJ3lUJCohJn/RnUeGWq2DNAoGAEkfLQcGvUT1yRdz7cubb7uAa08mYquNCx98VeL3xBozwIOJHs6ZEEjti75gKNS+SrQBrDQqHh7YDJgRnmthpuZbi02peEvO107pxtR3HAyG6+RvS9w8KHTfwl699ppnEkjkmQL7Q8Jwc0ngPrxqWsK4q8fG9ihVfTI/PnR5HpasCgYB9/ehwBktosTmclJ3Gjquywu1IfSImJeid8SA7e3D6ZISpNFS0dEY3GStvB7+V2Oci/RrsfJlZW0frVfR9AcZBdxnvfXbrc6MRL5e/jENePVPRSNfNb0uPa0Qb+iPCFbswSHVjaySu1U6b3hX2Acx75MXQu7GCCNLVqsTGtVio4w==","vddaC17dq1hmc"));
        contextMap.put(204L, new LinxiContext("262", "118.096164","忆江南","37.709021","MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3knPd7x8nBRiIlO9qY149ZKfbW3kvnWtsYMAhAd/WDJn1/7wEgc4mfzJqv6hdwsu919y+dC7JKRkf4Rgfw/59HWdR0V/4QtYLcORZc581FxZp4L/ea5Hov1heYDWt1NwIWoyzZMEYOeIWinKSgnmQYhfLvgVOHngvtukD7VG+n2gY1Sy8w5uHbHzTMWcB6ebA7KsKV9eAevX6O/pxJG6TiXdPitpjn9eYPIwnECixMXRkzV3Tc477ulvV3lUgP6+oL6Fc1HRzmy9Nkek5C+1fDb9kd2/4ZfoNArZ7E15FPFZwbQmsIpxKzkGNUi2OgSJLzEMAxOMbzDZSxn7NcAVawIDAQAB",
                "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQChkiNpgKmwftC6O5HoCejDtWvoVE+bPRNsEYN2W1zEFpLFuI1OTSREhlL4Z+CJM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s+Bo54CuYDQ6qQ36kLsee+v+Hz0rl/T6RPcWhmc5wyBgunWzRdQQUqK5SriqCK8bgOL4DBqVMw7AoGAEmV9rPlRkzKaG00qkEi8478AIHoyyQptiOdKieA0aykQpIO8gK/qh0RUQ4HFhq2XK5sMdssDvppT6T1oriGaWXARMng90drgWUUnZNW7rVmYRkfW7BhwVmTTjl0oOyWur2Tl7dQu+aAXCotxSzffv7c9cmRnxWt/2G9KYKxkLy0CgYEA1kP+ZLvaoJeskoTOyjPjl68REpkuZrbYFQnXNrzZW4eqLZguKZu8lza9/BiFtqZtVv0nwwaLGBOxLd/b+ooD/i4J5XoUiPKSJfK0h6aZPto0e3mDWT6l860WimLdqkWiINSW+yQ1EH6lL+DVv1WYGubibPGqVStTZxsEthNggSk=","vddahQqfxxgnH"));
        contextMap.put(208L, new LinxiContext("1498", "103.907996","曲靖12349平台","25.365688","MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3knPd7x8nBRiIlO9qY149ZKfbW3kvnWtsYMAhAd/WDJn1/7wEgc4mfzJqv6hdwsu919y+dC7JKRkf4Rgfw/59HWdR0V/4QtYLcORZc581FxZp4L/ea5Hov1heYDWt1NwIWoyzZMEYOeIWinKSgnmQYhfLvgVOHngvtukD7VG+n2gY1Sy8w5uHbHzTMWcB6ebA7KsKV9eAevX6O/pxJG6TiXdPitpjn9eYPIwnECixMXRkzV3Tc477ulvV3lUgP6+oL6Fc1HRzmy9Nkek5C+1fDb9kd2/4ZfoNArZ7E15FPFZwbQmsIpxKzkGNUi2OgSJLzEMAxOMbzDZSxn7NcAVawIDAQAB",
                "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCMdFd8XcKbVEg05K/zAyPHy47k7wlcx+GZp1pHpq/FospIYeantRTqcrCJQB/kWkE5POLzGwh4eP0i2SHa9oG5dDPvUncP6dtxXN1UKpri7Ha7nyzQbWVqDJQo25H+hKSbVF8RdVmC5QG8hD7ZcjJ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jBINH8dWNx3AbfGGElhdhYOdwJFbI4uYWUVuSjz5i813uFZL/K7UUrfZlVQXAA6oIm1UHC9Ws+BI11yfGA2VSEEEnBv/KbVlRDldKJci+2ycmAuq5I0GrAL+aOCyoLBG1OmDH2C5BzQBYQs0YGulP6gVlWxD1gNxIGRBAoGBALuIn16++zFkLveprP9+23Xqtg2wmc0fJ1ArYgcBxEkZuumLJO3VKaEMmH09W2LaWqhOlxJQ3rUzMBG4g2KhgSFUSQtNLkcX2NsNAIu/wqeDBK9SfVhV+USAdTXnNVJ1nRVWdkDBRkkivPsyPbM7GsiKHP+g5eQAQYby1qVzgIQE","vodarMgdHWnIx"));
        contextMap.put(198L, new LinxiContext("1659", "121.447755","中关村西区","37.464551","MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3knPd7x8nBRiIlO9qY149ZKfbW3kvnWtsYMAhAd/WDJn1/7wEgc4mfzJqv6hdwsu919y+dC7JKRkf4Rgfw/59HWdR0V/4QtYLcORZc581FxZp4L/ea5Hov1heYDWt1NwIWoyzZMEYOeIWinKSgnmQYhfLvgVOHngvtukD7VG+n2gY1Sy8w5uHbHzTMWcB6ebA7KsKV9eAevX6O/pxJG6TiXdPitpjn9eYPIwnECixMXRkzV3Tc477ulvV3lUgP6+oL6Fc1HRzmy9Nkek5C+1fDb9kd2/4ZfoNArZ7E15FPFZwbQmsIpxKzkGNUi2OgSJLzEMAxOMbzDZSxn7NcAVawIDAQAB",
                "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDwQpo6jjVeiixo1gA+qlC3SYSph6gFEiBtq4Y7LYyXyEa7u6iEvg9DHg0E01RpCgv10eGRPjKWuswjXotlBxz+AuMotCQFmBNydhVW/+XYiGtBU8AHTmuW/J5S97QSxmDeLi7XzsJ+2aWeZYVrYbarD1c/4cp7+8dNNXemx0gV5hr9IcNPQ9sWP/5YnX1tCH6OKv5MlkB8NBlYHk1x28K2FBmFNQ01xtzR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uBVOZ6beNcl7OPg8H6bgnsOm9bs4CPYyZSA2UV5kro/Zf8RHg9mdcRDxkbyhfOtnbOGngkHnKzLl5olLv18rug5GfStZAoGBALR3q50TPdoqhXj6aBXYsxPNMMiKjvYw9LsvmdzYDJaOJLEr0ofH3/2TDYJK3oN/qekxvRrHM8BfZ4EmZALUeqlIH6M97KjXRsMgGQWzM3B8saIj1XYoNGhnwL7mwrJrOApDslGW5xJGM1pEgCFdUFO6HAsFIRwR87y+F2qiHJ50","vodahHshY1jba"));
//		//测试环境
//		contextMap.put(198L, new LinxiContext("2", "121.447755","艾欧特园区","37.464551","MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3knPd7x8nBRiIlO9qY149ZKfbW3kvnWtsYMAhAd/WDJn1/7wEgc4mfzJqv6hdwsu919y+dC7JKRkf4Rgfw/59HWdR0V/4QtYLcORZc581FxZp4L/ea5Hov1heYDWt1NwIWoyzZMEYOeIWinKSgnmQYhfLvgVOHngvtukD7VG+n2gY1Sy8w5uHbHzTMWcB6ebA7KsKV9eAevX6O/pxJG6TiXdPitpjn9eYPIwnECixMXRkzV3Tc477ulvV3lUgP6+oL6Fc1HRzmy9Nkek5C+1fDb9kd2/4ZfoNArZ7E15FPFZwbQmsIpxKzkGNUi2OgSJLzEMAxOMbzDZSxn7NcAVawIDAQAB",
//		"MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCM68lhTOJDEiMP0Pb9X2aZfVKE/PolAiw0+iHoPmdHvKcyxe1DlKCtLgLGS/1LuzdV1R96daaRuEfkhFwQT6nrp31y4NLxK3hezxjb0TWY9jgPPJaA/GhpmQNTzAuvt4H602NCSwF+F06cxvu1ZK7X3S44gjz60V5ElvVgq/rL0HJqzo/6V7q70biUdfLadvcD48ltDNovZgYIgVotfBC+KPhnEqhaePTVH+Z48+noHZvEANCSexz7Pz2ImlTnDMrnj0//6sKE170JcHGMGBTZIkM2Z3oOQb3IwfOijZki6lbo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BgGgZjG4YHruGww/njA1NFeaXJurpYLVkMnjvzwYqY55hFit7f8bPh9A/3X9abj/TxzMcMLnpC9xDcJe0qU7LgOXxBsVcdtaDBrbY4LJ7Oc2rLLOuhSEX6waAX4HJLt//1JgY0G/PAbouarUNqrAGSbQBL7nB1vZjkOxBQ93LCKuS","vdda3mYa5bg9D"));
//		contextMap.put(198L, new LinxiContext("46", "121.447755","中关村西区","37.464551","MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3knPd7x8nBRiIlO9qY149ZKfbW3kvnWtsYMAhAd/WDJn1/7wEgc4mfzJqv6hdwsu919y+dC7JKRkf4Rgfw/59HWdR0V/4QtYLcORZc581FxZp4L/ea5Hov1heYDWt1NwIWoyzZMEYOeIWinKSgnmQYhfLvgVOHngvtukD7VG+n2gY1Sy8w5uHbHzTMWcB6ebA7KsKV9eAevX6O/pxJG6TiXdPitpjn9eYPIwnECixMXRkzV3Tc477ulvV3lUgP6+oL6Fc1HRzmy9Nkek5C+1fDb9kd2/4ZfoNArZ7E15FPFZwbQmsIpxKzkGNUi2OgSJLzEMAxOMbzDZSxn7NcAVawIDAQAB",
//		"MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCGJLKsTshwW3f1f+xqMcGA6NxUfFDKZSeHG1aitY0G7jqmBaEH4IHjEiqwI63Mu36WZ4x5e8boUF4xCegerwXJ2dUZdL1Gtbx9sta4te/L2yWFwHHZmW89TRETy/NVGEEqXyz4QLbwvphuzn9RI6/GegVcVJvHxcelTEBJ42S4/MSTMx/dDvk5R6eU7tkjRKrwO7egWb0vOodmuFXZp6gzXenpW08DQ29jqIONaQNsXvbhI06RJ6msNOoSOe604wtxUC9NHE8q+kT9K0nIe4Ulflp8f6vROLGrNrGQAJyjZfpdVgtsM8Zv1oz2wSPxLOzM6xsiHUSJbXqnLXzfkhnxAgMBAAECggEAbL6DObwEMVF1RceWV+BOBbrAIp1EwU6PZK985BpZYSzuznfx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t+b42Hh2uMpLbwsmL2pE4ngGQf030BBigs3ZTbqJ/Cd8ZJidiVshmkNPoJsUBbV/xE4onF4KqBMsQa1hGQR6iquLWKbTJA==","vodaqZv7aghQb"));
    }

    private static ObjectMapper objectMapper = new ObjectMapper();


    /**
     * 公钥加密
     *
     * @param content   要加密的内容
     * @param publicKey 平台公钥
     */
    public static String encrypt(String content, String publicKey) {
        RSA rsa = new RSA(null, publicKey);
        return rsa.encryptBase64(content, KeyType.PublicKey);
    }

    /**
     * 获取待签名字符串
     *
     * @return
     */
    public static String buildDataForm(Map params) {
        if (params.containsKey(SIGN)) {
            params.remove(SIGN);
        }
        StringBuilder sb = new StringBuilder();
        // 使用TreeMap对key按照字典升序排序
        TreeMap<String, Object> sortedMap = new TreeMap<>(params);
        // 拼接为key=value&key2=value2形式
        for (Map.Entry<String, Object> entry : sortedMap.entrySet()) {
            if (entry.getValue() == null) {
                // 空值不参与签名
                continue;
            }
            sb.append(entry.getKey()).append("=");
            sb.append(entry.getValue()).append("&");
        }
        sb.deleteCharAt(sb.lastIndexOf("&"));
        return sb.toString();
    }

    /**
     * 功能描述: 私钥签名
     *
     * @param params     签名参数
     * @param privateKey 客户私钥
     */
    public static String signByPri(String params, String privateKey) {
        log.info("待签名的参数为：：" + params);
        Sign sign = new Sign(SignAlgorithm.SHA1withRSA, privateKey, null);
        return java.util.Base64.getEncoder().encodeToString(sign.sign(params.getBytes(StandardCharsets.UTF_8)));
    }


    public static OtherJsonRequest getRequest(Map<String, Object> testMap, LinxiContext context) {
        /** ------------------------加密流程示例----------------------- **/
        /**
         * 生成AES秘钥
         */
        String keyStr = java.util.Base64.getEncoder().encodeToString(key.getEncoded());
        AES aes = new AES(Mode.ECB, Padding.ZeroPadding, key);
        OtherJsonRequest otherJsonRequest = new OtherJsonRequest();
        //rsa 对ase秘钥加密
        otherJsonRequest.setAesKey(encrypt(keyStr, context.getCity()));
        //使用ase秘钥对参数进行加密
        String jsonStr = JSONUtil.toJsonStr(testMap);
        log.info("加密前参数：" + jsonStr);
        otherJsonRequest.setBody(aes.encryptBase64(jsonStr, StandardCharsets.UTF_8));
        log.info("加密后的请求报文：：：" + aes.encryptBase64(buildDataForm(testMap), StandardCharsets.UTF_8));
        //存放时间和appid
        otherJsonRequest.setAppId(context.getAppid());
        otherJsonRequest.setTimestamp(System.currentTimeMillis());
        otherJsonRequest.setSign(signByPri(buildDataForm(BeanUtil.beanToMap(otherJsonRequest)), context.getProvince()));
        return otherJsonRequest;
    }

    public static  <T> String toJson(T obj) {
        if (obj == null){
            return null;
        }
        try {
            return obj instanceof String ? (String) obj:objectMapper.writeValueAsString(obj);
        } catch (IOException e) {
            log.warn("Parse object to String error", e);
            return null;
        }
    }

    public static  <T> String toPrettyJson(T obj) {
        if (obj == null){
            return null;
        }
        try {
            return obj instanceof String ? (String) obj:objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
        } catch (IOException e) {
            log.warn("Parse object to String error", e);
            return null;
        }
    }
}

